import { Injectable } from '@angular/core';

@Injectable()
export class FormatDateService {
    date: Object;
    time: Object;
    hourFormat = '24';
    locale = {
        firstDayOfWeek: 0,
        dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
        dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
        dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
        monthNames: ['January', 'February', 'March', 'April', 'May', 'June',
            'July', 'August', 'September', 'October', 'November', 'December'],
        monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    };
    constructor() {
        const now = new Date();
        this.date = { year: now.getFullYear(), month: now.getMonth() + 1, day: now.getDate() };
        this.time = { hour: now.getHours(), minute: now.getMinutes(), second: now.getSeconds() };
    }

    // 格式化日期
    formatDate(date: Date, format: string) {
        if (!date) {
            return '';
        }
        let iFormat: any, lookAhead = function (match: any) {
            let matches = (iFormat + 1 < format.length && format.charAt(iFormat + 1) === match);
            if (matches) {
                iFormat++;
            }
            return matches;
        }, formatNumber = function (match: any, value: any, len: any) {
            let num = '' + value;
            if (lookAhead(match)) {
                while (num.length < len) {
                    num = '0' + num;
                }
            }
            return num;
        }, formatName = function (match: any, value: any, shortNames: any, longNames: any) {
            return (lookAhead(match) ? longNames[value] : shortNames[value]);
        }, output = '', literal = false;
        if (date) {
            for (iFormat = 0; iFormat < format.length; iFormat++) {
                if (literal) {
                    if (format.charAt(iFormat) === "'" && !lookAhead("'")) {
                        literal = false;
                    } else {
                        output += format.charAt(iFormat);
                    }
                } else {
                    switch (format.charAt(iFormat)) {
                        case "d":
                            output += formatNumber("d", date.getDate(), 2);
                            break;
                        case "D":
                            output += formatName("D", date.getDay(), this.locale.dayNamesShort, this.locale.dayNames);
                            break;
                        case "o":
                            output += formatNumber("o", Math.round((new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime() - new Date(date.getFullYear(), 0, 0).getTime()) / 86400000), 3);
                            break;
                        case "m":
                            output += formatNumber("m", date.getMonth() + 1, 2);
                            break;
                        case "M":
                            output += formatName("M", date.getMonth(), this.locale.monthNamesShort, this.locale.monthNames);
                            break;
                        case "y":
                            output += (lookAhead("y") ? date.getFullYear() :
                                (date.getFullYear() % 100 < 10 ? "0" : "") + date.getFullYear() % 100);
                            break;
                        case "@":
                            output += date.getTime();
                            break;
                        case "'":
                            if (lookAhead("'")) {
                                output += "'";
                            } else {
                                literal = true;
                            }
                            break;
                        default:
                            output += format.charAt(iFormat);
                    }
                }
            }
        }
        return output;
    };

    formatTime(date: Date, format: string) {
        if (!date) {
            return '';
        }
        let output = '';
        let hours = date.getHours();
        let minutes = date.getMinutes();
        let secondes = date.getSeconds();
        if (this.hourFormat === '12' && hours !== 12) {
            hours -= 12;
        }
        output += (hours < 10) ? '0' + hours : hours;
        output += format;
        output += (minutes < 10) ? '0' + minutes : minutes;
        output += format;
        output += (secondes < 10) ? '0' + secondes : secondes;
        return output;
    }

    /** 
     * Returns 20170119133000
     * @param date = { year: now.getFullYear(), month: now.getMonth() + 1, day: now.getDate() };
     *  @param time = { hour: now.getHours(), minute: now.getMinutes(), second: 0 };
     *  */
    parseDateTime(date: any, time: any): string {
        let output = '';
        output += date.year.toString();
        output += (date.month < 10) ? '0' + date.month : date.month + '';
        output += (date.day < 10) ? '0' + date.day : date.day + '';
        output += (time.hour < 10) ? '0' + time.hour : time.hour + '';
        output += (time.minute < 10) ? '0' + time.minute : time.minute + '';
        output += (time.second < 10) ? '0' + time.second : time.second + '';
        return output;
    }

    /** 
     * Returns 20170119133000
     * @param date = { year: now.getFullYear(), month: now.getMonth() + 1, day: now.getDate() };
     *  @param time = { hour: now.getHours(), minute: now.getMinutes(), second: 0 };
     *  */
    parseDate(date: any): string {
        let output = '';
        output += date.year.toString();
        output += (date.month < 10) ? '0' + date.month : date.month + '';
        output += (date.day < 10) ? '0' + date.day : date.day + '';
        return output;
    }
}